package vip.yyzhu.redisadmin.config;


import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.NoHandlerFoundException;
import vip.yyzhu.redisadmin.jopo.ResJson;

import java.util.HashMap;
import java.util.Map;

/**
 * controller 增强器
 *
 * @author sam
 * @since 2017/7/17
 */
@ControllerAdvice
public class MyControllerAdvice {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(Exception ex) {

        // if (ex instanceof NoHandlerFoundException) {
        //     System.out.println("404错误");
        // } else {
        //     System.out.println("未知错误");
        // }
        // System.out.println(ex);
        Map map = new HashMap();
        map.put("err",ex.getMessage());
        return new ResJson(4444, "服务器内部错误", map);
    }
}